<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\User;
use Auth;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use Validator;


class UserController extends Controller
{
    use AuthenticatesUsers;
    public $successStatus = 200;

    /**
     * login api.
     *
     * @return \Illuminate\Http\Response
     */
    public function login(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'email' => 'required|email',
            'password' => 'required',
        ]);
        if ($validator->fails()) {
            return response()->json(['error' => $validator->errors()], 401);
        }
        if (Auth::attempt(['email' => request('email'), 'password' => request('password')])) {
            $user = Auth::user();
            $data = ['success' => true, 'message' => '注册成功', 'email' => $user->email, 'id' => $user->id, 'token' => $user->createToken('MyApp')->accessToken];
            session()->flash('true', '登陆成功');

            return json_encode($data);
        } else {
            session()->flash('false', '登录失败，账户或密码错误');

            return redirect()->back();
        }
    }

    /**
     * Register api.
     *
     * @return \Illuminate\Http\Response
     */
    public function register(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'email' => 'required|email',
            'password' => 'required|min:6',
            'name' => 'required',
        ]);

        if ($validator->fails()) {
            return response()->json(['error' => $validator->errors()], 401);
        }
        $email = User::where('email', $request->email)
            ->first();
        if ($email) {
            session()->flash('false', '此邮箱已被注册！');

            return redirect()->back();
        }
        $input = $request->all();
        // var_dump($input);
        $input['password'] = bcrypt($input['password']);
        // var_dump($input);
        $user = User::create($input);
        // var_dump($input);
        $data = ['success' => true, 'message' => '注册成功', 'email' => $user->email, 'token' => $user->createToken('MyApp')->accessToken];

        return json_encode($data);
    }

    /**
     * details api.
     *
     * @return \Illuminate\Http\Response
     */
    public function Update(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'email' => 'required|email',
            'password' => 'required',
            'name' => 'required',
            'id' => 'required',
        ]);

        if ($validator->fails()) {
            return response()->json(['error' => $validator->errors()], 401);
        }
        $user = $request->all();
        $res = User::where('id', '=', $request->id)->frist($user);
        if ($res == $user) {
            session()->flash('false', '修改内容与之前重复!');
            return redirect()->back();
        }
        $re = User::where('id', '=', $request->id)->update($user);
        if ($re) {
            session()->flash('true', '修改成功！');
            $data = ['success' => true, 'message' => '修改成功'];

            return json_encode($data);
        } else {
            session()->flash('false', '修改失败，无对应ID信息!');
            return redirect()->back();
        }
    }

    public function SelfPage(User $user)
    {
        return json_encode($user);
    }
}
